package com.cskaoyan.market.service.impl;

import com.cskaoyan.market.service.AdminOrderService;
import com.cskaoyan.market.db.admin.vo.CourierVO;
import com.cskaoyan.market.db.admin.vo.OrderDetailVO;
import com.cskaoyan.market.db.admin.vo.OrderInnerOrderDetailVo;
import com.cskaoyan.market.db.admin.vo.UserInfoInnerOrderDetailVO;
import com.cskaoyan.market.db.common.BasePageInfo;
import com.cskaoyan.market.db.common.CommonData;
import com.cskaoyan.market.db.domain.*;
import com.cskaoyan.market.db.mapper.MarketOrderGoodsMapper;
import com.cskaoyan.market.db.mapper.MarketOrderMapper;
import com.cskaoyan.market.db.mapper.MarketUserMapper;
import com.cskaoyan.market.util.AdminOrderStatusCode;
import com.cskaoyan.market.util.MyBatisUtil;
import com.cskaoyan.market.util.ReflectUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.session.SqlSession;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 小焦
 * @since 2023/03/20 15:12
 */
public class AdminOrderServiceImpl implements AdminOrderService {

    @Override
    public CommonData<MarketOrder> list(BasePageInfo basePageInfo, String start, String end, String[] orderStatusArray,
        String userId, String orderSn) {
        SqlSession sqlSession = MyBatisUtil.getSession();
        MarketOrderMapper orderMapper = sqlSession.getMapper(MarketOrderMapper.class);

        // 开启分页插件
        PageHelper.startPage(basePageInfo.getPage(), basePageInfo.getLimit());
        MarketOrderExample orderExample = new MarketOrderExample();
        MarketOrderExample.Criteria criteria = orderExample.createCriteria();
        // 首先根据日期查询
        if (!StringUtils.isEmpty(start)) {
            LocalDate startDate = LocalDate.parse(start);
            criteria.andPayTimeGreaterThanOrEqualTo(startDate.atTime(00, 00, 00));
        }
        if (!StringUtils.isEmpty(end)) {
            LocalDate endDate = LocalDate.parse(end);
            criteria.andPayTimeLessThanOrEqualTo(endDate.atTime(00, 00, 00));
        }
        // 根据订单状态查询
        if (orderStatusArray != null && orderStatusArray.length > 0) {
            List<Short> status = new ArrayList<>();
            for (String orderStatu : orderStatusArray) {
                status.add(Short.valueOf(orderStatu));
            }
            criteria.andOrderStatusIn(status);
        }
        // userId
        if (!StringUtils.isEmpty(userId)) {
            criteria.andUserIdEqualTo(Integer.valueOf(userId.trim()));
        }
        // 订单编号
        if (!StringUtils.isEmpty(orderSn)) {
            criteria.andOrderSnEqualTo(orderSn.trim());
        }
        List<MarketOrder> marketOrders = orderMapper.selectByExample(orderExample);
        PageInfo<MarketOrder> pageInfo = new PageInfo<>(marketOrders);
        CommonData<MarketOrder> data = CommonData.data(pageInfo);
        // orderMapper.selectByExample()

        // if()
        return data;
    }

    @Override
    public OrderDetailVO detail(String orderId) {
        SqlSession sqlSession = MyBatisUtil.getSession();
        MarketOrderMapper orderMapper = sqlSession.getMapper(MarketOrderMapper.class);
        MarketUserMapper userMapper = sqlSession.getMapper(MarketUserMapper.class);
        MarketOrderGoodsMapper orderGoodsMapper = sqlSession.getMapper(MarketOrderGoodsMapper.class);
        Integer id = Integer.valueOf(orderId);
        MarketOrder order = orderMapper.selectByPrimaryKey(id);
        OrderInnerOrderDetailVo orderVo = ReflectUtils.copyObject(order, OrderInnerOrderDetailVo.class);

        MarketUser marketUser = userMapper.selectByPrimaryKey(order.getUserId());
        UserInfoInnerOrderDetailVO user = new UserInfoInnerOrderDetailVO();
        user.setNickname(marketUser.getNickname());
        user.setAvatar(marketUser.getAvatar());
        MarketOrderGoodsExample orderGoodsExample = new MarketOrderGoodsExample();
        MarketOrderGoodsExample.Criteria criteria = orderGoodsExample.createCriteria();
        criteria.andOrderIdEqualTo(id);

        List<MarketOrderGoods> orderGoodsList = orderGoodsMapper.selectByExample(orderGoodsExample);
        // 关闭sqlSession
        sqlSession.commit();
        sqlSession.close();

        OrderDetailVO orderDetailVO = new OrderDetailVO();
        orderDetailVO.setOrder(orderVo);
        orderDetailVO.setOrderGoods(orderGoodsList);
        orderDetailVO.setUser(user);
        return orderDetailVO;
    }

    @Override
    public List<CourierVO> channel() {
        List<CourierVO> list = new ArrayList<>();
        list.add(new CourierVO("ZTO", "中通快递"));
        list.add(new CourierVO("YTO", "圆通速递"));
        list.add(new CourierVO("YD", "韵达速递"));
        list.add(new CourierVO("YZPY", "邮政快递包裹"));
        list.add(new CourierVO("EMS", "EMS"));
        list.add(new CourierVO("DBL", "德邦快递"));
        list.add(new CourierVO("FAST", "快捷快递"));
        list.add(new CourierVO("ZJS", "宅急送"));
        list.add(new CourierVO("TNT", "TNT快递"));
        list.add(new CourierVO("UPS", "UPS"));
        list.add(new CourierVO("DHL", "DHL"));
        list.add(new CourierVO("FEDEX", "FEDEX联邦(国内件)"));
        list.add(new CourierVO("FEDEX_GJ", "FEDEX联邦(国际件)"));

        return list;
    }

    @Override
    public void updateOrderStatusById(String orderId, String shipChannel, String shipSn) {
        SqlSession sqlSession = MyBatisUtil.getSession();
        MarketOrderMapper orderMapper = sqlSession.getMapper(MarketOrderMapper.class);

        // MarketOrderExample marketOrderExample = new MarketOrderExample ();
        MarketOrder marketOrder = new MarketOrder();
        marketOrder.setId(Integer.valueOf(orderId));
        marketOrder.setShipChannel(shipChannel);
        marketOrder.setShipSn(shipSn);
        marketOrder.setOrderStatus(AdminOrderStatusCode.ORDER_SENDED);
        marketOrder.setShipTime(LocalDateTime.now());

        int i = orderMapper.updateByPrimaryKeySelective(marketOrder);
        // MarketOrder marketOrder = new MarketOrder ();
        // orderMapper.updateOrderStatusByPrimaryKey ( Integer.valueOf ( orderId ) );
        sqlSession.commit();
        sqlSession.close();

    }

    @Override
    public void updateOrderStatusByIdAmount(String orderId, String refundAmount) {
        SqlSession sqlSession = MyBatisUtil.getSession();
        MarketOrderMapper orderMapper = sqlSession.getMapper(MarketOrderMapper.class);
        MarketOrder marketOrder = new MarketOrder();
        marketOrder.setId(Integer.valueOf(orderId));
        BigDecimal bd = new BigDecimal(refundAmount);
        bd = bd.setScale(2, BigDecimal.ROUND_HALF_UP);
        marketOrder.setRefundAmount(bd);
        marketOrder.setOrderStatus(AdminOrderStatusCode.ORDER_REFNDED);
        marketOrder.setRefundTime(LocalDateTime.now());
        marketOrder.setRefundType(AdminOrderStatusCode.REFUND_TYPE);
        marketOrder.setAftersaleStatus(AdminOrderStatusCode.AFTERSALES_MANAGER_PASS);
        int i = orderMapper.updateByPrimaryKeySelective(marketOrder);
        sqlSession.commit();
        sqlSession.close();
    }

    @Override
    public void deleteOrderById(String orderId) {
        SqlSession sqlSession = MyBatisUtil.getSession();
        MarketOrderMapper orderMapper = sqlSession.getMapper(MarketOrderMapper.class);
        int i = orderMapper.deleteByPrimaryKey(Integer.valueOf(orderId));
        sqlSession.commit();
        sqlSession.close();
    }
}
